<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    table {
      width: 500px;
    }

    td {
      text-align: center;
    }
  </style>
</head>

<body>
  ID: <input type="text" id="cardid">
  姓名: <input type="text" id="user">
  电话： <input type="text" id="tel">
  <button id="btn">保存</button>
  <div>
    <table border="1">
      <thead>
        <tr>
          <th>ID</th>
          <th>NAME</th>
          <th>TEL</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody id="mytbody">
        <!-- <tr>
          <td>1</td>
          <td>张三</td>
          <td>123131</td>
          <td><a href="#">删除</a></td>
        </tr> -->
      </tbody>
    </table>
  </div>
  <script>
    var btn = document.getElementById("btn")
    btn.onclick = function () {
      var tr = document.createElement("tr");//创建tr
      // id生成td并赋值
      var cardidTd = document.createElement("td");
      var cardid = document.getElementById("cardid").value;
      cardidTd.innerHTML = cardid;
      tr.appendChild(cardidTd)
      // 用户
      var userTd = document.createElement("td");
      var userVal = document.getElementById("user").value;
      userTd.innerHTML = userVal;
      tr.appendChild(userTd)
      // 电话
      var telTd = document.createElement("td");
      var telVal = document.getElementById("tel").value;
      telTd.innerHTML = telVal;
      tr.appendChild(telTd)
      // 操作的td
      var deltd = document.createElement("td");
      deltd.innerHTML = "<a href='#'  class='del'>删除</a>"
      tr.appendChild(deltd)
      // 把生成的tr放到tbody
      var mytbody = document.getElementById("mytbody");
      mytbody.appendChild(tr)
      // 生成tr后调用删除方法
      del()
    }
    function del() {
      var dels = document.querySelectorAll(".del");
      for (var i = 0; i < dels.length; i++) {
        dels[i].onclick = function () {
          var tr = this.parentNode.parentNode;
          tr.remove();
        }
      }
    }

  </script>
</body>

</html>